const app = getApp()
const request = require('../../utils/promise.js');
const common = require('../../utils/common.js');

Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    WebUrl: app.globalData.weburl,
    contentDetail: null,
    videolist: [],
    contentId: null,
    videoId: null,
    videoPath: '',
    videoTitle: '',
    videoPrice: '',
    userInfo: [],
    checkOrder: 0,
  },
  onLoad: function (option) {
    console.log('parmindex', option.id);
    this.setData({
      contentId: option.id
    });
    this.loadLession(option.id);
    this.loadVideos(option.id);
    var openid = wx.getStorageSync('openid');
    if (openid != "") {
      this.getUserInfo(openid);

    }
    setTimeout(function () {
      console.log('checkOrder');

    }, 1000)
  },
  loadLession: function (val) {
    var parm = {
      "contentId": val,
    };
    request.GET("/mdz/content/contentdetail", parm).then(res => {
      console.log('contentdetail', res.data);
      if (res.data.code == "200") {
        this.setData({
          contentDetail: res.data.data
        });
      }
      else {
        common.CheckErrorForBusiness(res.data.message);
      }
    })
  },
  loadVideos: function (val) {
    var parm = {
      "contentId": val,
    };
    request.GET("/mdz/content/videolist", parm).then(res => {
      console.log('videolist', res.data);
      if (res.data.code == "200") {
        this.setData({
          videolist: res.data.rows,
        });
        // console.log('this.data.videoId',this.data.videoId);
        if (this.data.videoId == null) {
          this.setData({
            videoId: res.data.rows[0].videoId,
            videoPath: res.data.rows[0].videoPath,
            videoTitle: res.data.rows[0].videoTitle,
            videoPrice: res.data.rows[0].price,
          });
        }
        else {
          for (var i = 0; i < this.data.videolist.length; i++) {
            if (this.data.videolist[i].videoId == this.data.videoId) {
              console.log(this.data.videolist[i]);
              this.setData({
                videoPath: this.data.videolist[i].videoPath,
                videoTitle: this.data.videolist[i].videoTitle,
                videoPrice: this.data.videolist[i].price,
              });
            }
          }
        }
        var openid = wx.getStorageSync('openid');
        if (openid != "") {
          this.checkOrder(openid)
        }

        //观看历史
        let historyArr = wx.getStorageSync('history') ?? [];
        // 检查 historyArr 是否是数组，如果不是则初始化为空数组  
        if (!Array.isArray(historyArr)) {
          historyArr = [];
        }
        const newHistoryItem = val;
        // 检查数组中是否已存在该历史记录  
        const historyIndex = historyArr.indexOf(newHistoryItem);
        if (historyIndex !== -1) {
          // 如果存在，则将该值从当前位置移除，并插入到数组的最前面  
          historyArr.splice(historyIndex, 1); // 移除  
          historyArr.unshift(newHistoryItem); // 插入到最前面  
        } else {
          // 如果不存在，则直接添加到数组末尾  
          historyArr.unshift(newHistoryItem);
        }
        wx.setStorageSync('history', historyArr);
        console.log('history', historyArr);
      }
      else {
        common.CheckErrorForBusiness(res.data.message);
      }
    })
  },
  openVideo: function (event) {
    this.loadLession(this.data.contentId);
    this.loadVideos(this.data.contentId);

    // console.log('openVideo', event.currentTarget.dataset.index);
    this.setData({
      videoId: event.currentTarget.dataset.index
    });
    // console.log('contentId',this.data.contentDetail.contentId);
    wx.pageScrollTo({
      scrollTop: 0,
    });
  },
  getUserInfo: function (openid) {
    var parm = {
      "openid": openid,
    };

    request.GET("/mdz/reguser/getuserinfo", parm).then(res => {
      console.log(res);
      if (res.data.code == "200") {
        this.setData({
          userInfo: res.data.data,
        });
      }
      else {
        common.CheckErrorForBusiness(res.data.msg);
      }
    })
  },
  checkOrder: function (openid) {
    var parm = {
      "openid": openid,
      "contentId": this.data.contentId,
      "videoId": this.data.videoId,
    };

    request.POST("/mdz/order/checkOrder", parm).then(res => {
      console.log('checkOrder', res);
      if (res.data.code == "200") {
        this.setData({
          checkOrder: res.data.msg,
        });
      }
      else {
        common.CheckErrorForBusiness(res.data.msg);
      }
    })
  },
  addOrder: function (event) {
    var orderType = event.currentTarget.dataset.index;
    console.log('addOrder', orderType);

    var openid = wx.getStorageSync('openid');
    if (openid == "") {
      wx.showToast({
        icon: 'none',
        title: '请先登录',
        duration: 2000
      })
      return
    }

    var parm = {
      "openid": openid,
      "orderType": orderType + "",
      "contentId": this.data.contentId,
      "videoId": this.data.videoId,
    };

    request.POST("/mdz/order/addOrder", parm).then(res => {
      console.log(res);
      if (res.data.code == "200") {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
          duration: 4000
        })
        this.getUserInfo(openid);
      }
      else {
        common.CheckErrorForBusiness(res.data.msg);
      }
    })
  },

})
